package com.rongzhe.house.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.Dictionary;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.DictionaryManager;
import com.rongzhe.house.manager.HouseManager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by huang on 2017/11/27.
 */

public class FilterRentTypeFragment extends FilterFragment
        implements CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.btn_rent_type_share_unlimuted)
    CheckBox btnRentTypeShareUnlimuted;
    @BindView(R.id.btn_rent_type_share_2)
    CheckBox btnRentTypeShare2;
    @BindView(R.id.btn_rent_type_share_3)
    CheckBox btnRentTypeShare3;
    @BindView(R.id.btn_rent_type_share_more_3)
    CheckBox btnRentTypeShareMore3;
    @BindView(R.id.btn_rent_type_all_unlimuted)
    CheckBox btnRentTypeAllUnlimuted;
    @BindView(R.id.btn_rent_type_all_2)
    CheckBox btnRentTypeAll2;
    @BindView(R.id.btn_rent_type_all_3)
    CheckBox btnRentTypeAll3;
    @BindView(R.id.btn_rent_type_all_more_3)
    CheckBox btnRentTypeAllMore3;

    Unbinder unbinder;
    @BindView(R.id.btn_reset)
    Button btnReset;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.btn_rent_type_share_more_4)
    CheckBox btnRentTypeShareMore4;
    @BindView(R.id.btn_rent_type_all_more_4)
    CheckBox btnRentTypeAllMore4;
    private List<CompoundButton> mCheckedItems = new ArrayList<>();
    private List<CompoundButton> mSharedTypeItems = new ArrayList<>();
    private List<CompoundButton> mFullTypeItems = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_filter_rent_type, container, false);
        unbinder = ButterKnife.bind(this, view);
        mSharedTypeItems.add(btnRentTypeShareUnlimuted);
        mSharedTypeItems.add(btnRentTypeShare2);
        mSharedTypeItems.add(btnRentTypeShare3);
        mSharedTypeItems.add(btnRentTypeShareMore3);
        mSharedTypeItems.add(btnRentTypeShareMore4);
        mFullTypeItems.add(btnRentTypeAllUnlimuted);
        mFullTypeItems.add(btnRentTypeAll2);
        mFullTypeItems.add(btnRentTypeAll3);
        mFullTypeItems.add(btnRentTypeAllMore3);
        mFullTypeItems.add(btnRentTypeAllMore4);
        loadHouseTypeDictionary();
        return view;
    }

    private void loadHouseTypeDictionary() {
        DictionaryManager.getInstance().getDictionaryByType(Dictionary.Companion.getHOUSE_ROOM_NUM(),
                new DataListener<List<Dictionary>>() {
                    @Override
                    public void onSuccess(List<Dictionary> data, String message) {
                        initHouseType(data);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {

                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    public void initHouseType(List<Dictionary> dictionaries) {
        if (dictionaries == null) {
            return;
        }
        for (int i = 0; i < dictionaries.size(); i++) {
            Dictionary dictionary = dictionaries.get(i);
            //因为第0项是不限, 所以跳过第零项
            int itemPositon = i + 1;
            if (mSharedTypeItems.size() > itemPositon) {
                mSharedTypeItems.get(itemPositon).setText(dictionary.getName());
                mSharedTypeItems.get(itemPositon).setTag(dictionary);
            }
            if (mFullTypeItems.size() > itemPositon) {
                mFullTypeItems.get(itemPositon).setText(dictionary.getName());
                mFullTypeItems.get(itemPositon).setTag(dictionary);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnCheckedChanged({R.id.btn_rent_type_share_unlimuted, R.id.btn_rent_type_share_2,
            R.id.btn_rent_type_share_3, R.id.btn_rent_type_share_more_3,
            R.id.btn_rent_type_share_more_4,
            R.id.btn_rent_type_all_unlimuted, R.id.btn_rent_type_all_2,
            R.id.btn_rent_type_all_3, R.id.btn_rent_type_all_more_3,
            R.id.btn_rent_type_all_more_4
    })
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            mCheckedItems.add(buttonView);
            if (mCheckedItems.size() > 0) {
                btnReset.setEnabled(true);
                btnReset.setEnabled(true);
            }
            //整租 合租只能选择一个
            if (mSharedTypeItems.indexOf(buttonView) != -1) {
                setAsUnChecked(mFullTypeItems, false);
            } else {
                setAsUnChecked(mSharedTypeItems, false);
            }
            //这里的逻辑是 不限与后面的项冲突
            if (mSharedTypeItems.indexOf(buttonView) == 0) {
                setAsUnChecked(mSharedTypeItems, true);
            } else if (mSharedTypeItems.indexOf(buttonView) != -1) {
                mSharedTypeItems.get(0).setChecked(false);
            } else if (mFullTypeItems.indexOf(buttonView) == 0) {
                setAsUnChecked(mFullTypeItems, true);
            } else if (mFullTypeItems.indexOf(buttonView) != -1) {
                mFullTypeItems.get(0).setChecked(false);
            }

        } else {
            if (!mInItrator) {
                mCheckedItems.remove(buttonView);
            }
            if (mCheckedItems.size() < 1) {
                btnReset.setEnabled(false);
                btnReset.setEnabled(false);
            }
        }
    }

    private void setAsUnChecked(List<CompoundButton> items, boolean ignoreFirst) {
        boolean first = true;
        for (CompoundButton compoundButton : items) {
            if (first && ignoreFirst) {
                first = false;
                continue;
            }
            compoundButton.setChecked(false);
        }
    }

    //为什么要加这个呢, 是因为在遍历设置checked的过程中会触发onCheckedChanged. 导致报修改正在遍历的list的错误
    private boolean mInItrator = false;

    @OnClick(R.id.btn_reset)
    public void onResetClick() {
        Iterator<CompoundButton> compoundButtonIt = mCheckedItems.iterator();
        mInItrator = true;
        while (compoundButtonIt.hasNext()) {
            compoundButtonIt.next().setChecked(false);
        }
        mInItrator = false;
        mCheckedItems.clear();
        HouseManager.getInstance().setAsEntire(null);
        HouseManager.getInstance().clearHouseType();
        finishFragment();
    }

    @OnClick(R.id.btn_ok)
    public void onOkClick() {
        if (mCheckedItems.size() == 0) {
            HouseManager.getInstance().setAsNoTypeFilter();
        }
        HouseManager.getInstance().clearHouseType();
        for (CompoundButton compoundButton : mCheckedItems) {
            HouseManager houseManager = HouseManager.getInstance();
            houseManager.setAsEntire(mSharedTypeItems.indexOf(compoundButton) == -1);
            if(compoundButton.getTag() != null) {
                houseManager.addHouseQueryType(((Dictionary) compoundButton.getTag()).getCode());
            }
        }
        finishFragment();
    }
}
